﻿using JiebaNet.Analyser;
using JiebaNet.Segmenter;
using JiebaNet.Segmenter.Common;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Mvc;

namespace Job.Controllers
{
    public class HomeController : Controller
    {
        JobDataContext db = new JobDataContext();
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult About()
        {
            ViewBag.Message = "Your application description page.";

            return View();
        }

        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }

        public ActionResult Test()
        {
            //var startTxt = (from p in db.positions
            //                select p).FirstOrDefault();
            //ViewBag.startTxt = startTxt.descriptText;

            //JiebaSegmenter segmenter = new JiebaSegmenter();
            //var segments = segmenter.Cut(startTxt.descriptText);
            //ViewBag.tranTxt = string.Join("/", segments);

            //TfidfExtractor extractor = new TfidfExtractor();
            //var t = extractor.ExtractTags(string.Join("/", segments).Trim(),30,null);
            //ViewBag.endTxt = string.Join("/",t);
            //jieba();

            gotest();
            return View();
        }

        public void gotest()
        {
            var ruselt = (from r in db.positions
                         where r.searchKeyWord == "java"
                         where r.Salary != ""
                         select r).ToList();
            //double startNum = 0.0;
            //double endNum = 0.0;
            //int error = 0;
            for(int i =0;i<ruselt.Count();i++)
            {
                MatchCollection mc = Regex.Matches(ruselt[i].Salary, @"\d{1,2}\.\d{1,2}|\d{1,3}");
                if (Regex.Match(ruselt[i].Salary, @"(\d{1,}|\d{1}.\d{1,2})-(\d{1,}|\d{1}.\d{1,2})万/月").Success)
                {
                    //startNum += Convert.ToDouble(mc[0].Value) * 10.0;
                    //endNum += Convert.ToDouble(mc[1].Value) * 10.0;
                    ruselt[i].Salary = ((Convert.ToDouble(mc[0].Value) * 10.0 + Convert.ToDouble(mc[1].Value) * 10.0) / 2).ToString();
                }
                else if (Regex.Match(ruselt[i].Salary, @"(\d{1,}|\d{1}.\d{1,2})-(\d{1,}|\d{1}.\d{1,2})万/年").Success)   // 匹配模式：__-__万/年
                {
                    //startNum += Convert.ToDouble(mc[0].Value) * 10.0 / 12;
                    //endNum += Convert.ToDouble(mc[1].Value) * 10.0 / 12;
                    ruselt[i].Salary = ((Convert.ToDouble(mc[0].Value) * 10.0/12 + Convert.ToDouble(mc[1].Value) * 10.0/12) / 2).ToString();
                }
                else if (Regex.Match(ruselt[i].Salary, @"(\d{1,}|\d{1}.\d{1,2})-(\d{1,}|\d{1}.\d{1,2})千/月").Success)     // 匹配模式：__-__千/月
                {
                    //startNum += Convert.ToDouble(mc[0].Value);
                    //endNum += Convert.ToDouble(mc[1].Value);
                    ruselt[i].Salary = ((Convert.ToDouble(mc[0].Value) + Convert.ToDouble(mc[1].Value) ) / 2).ToString();
                }
                
            }
            db.SubmitChanges();
        }

        public void jieba()
        {
            // 先将数据按照分组取出
            var positions = from p in db.positions
                            group p by p.searchKeyWord into g
                            select g;
            string[] arr = { "PHP", "JAVA", "NET" };
            // 添加自定义词典
            var seg = new JiebaSegmenter();
            seg.LoadUserDict("D://user_dict_file_path.txt");

            int i = 0;
            // 循环列表，对每一个分组进行统计
            foreach (var groupKey in positions)
            {
                // 将所有的职位要求，拼接成一个字符串
                StringBuilder sb = new StringBuilder();
                foreach (var item in groupKey)
                {
                    sb.Append(item.descriptText.Replace(" ", "").Replace("\n", "")
                        .Replace("微信分享", String.Empty)
                        .Replace("phpphp", "PHP").Replace("php", "PHP")
                        .Replace("sql", "SQL").Replace("Sql", "SQL")
                        .Replace("SQLSERVER", "SQLServer").Replace("SQLserver", "SQLServer").Replace("SQL-Server", "SQLServer")
                        .Replace("mysql", "MySQL").Replace("Mysql", "MySQL").Replace("MYSQL", "MySQL").Replace("mySQL", "MySQL")
                        .Replace("oracle", "Oracle")
                        .Replace("java", "JAVA").Replace("Java", "JAVA")
                        .Replace("JAVAScript", "JavaScript").Replace("JAVAscript", "JavaScript").Replace("javascript", "JavaScript").Replace("js", "JavaScript").Replace("JAVASCRIPT", "JavaScript").Replace("JS", "JavaScript").Replace("javaScript", "JavaScript")
                        .Replace("HTML5", "HTML").Replace("html", "HTML").Replace("H5", "HTML")
                        .Replace("c#", "C#")
                        .Replace("web", "Web").Replace("WEB", "Web")
                        .Replace("Web服务", "WebService")
                        .Replace("linux", "Linux")
                        .Replace("redis", "Redis")
                        .Replace("spring", "Spring")
                        .Replace("Springboot", "SpringBoot")
                        .Replace("Springcloud", "SpringCloud")
                        .Replace("Springmvc", "SpringMVC").Replace("SpringMvc", "SpringMVC")
                        .Replace("thinkPHP", "ThinkPHP")
                        .Replace("laravel", "Laravel")
                        .Replace("Mybatis", "MyBatis").Replace("mybatis", "MyBatis")
                        .Replace(".net", ".NET").Replace("Net", "NET").Replace("net", "NET")
                        .Replace("NETCore", ".NETCore").Replace(".NetCore", ".NETCore").Replace("Core", ".NETCore")
                        .Replace("css", "CSS").Replace("Css", "CSS").Replace("CSS3", "CSS")
                        .Replace("hibernate", "Hibernate")
                        .Replace("AJAX", "Ajax").Replace("ajax", "Ajax")
                        .Replace("Jquery", "JQuery").Replace("jquery", "JQuery").Replace("jq", "JQuery")
                        .Replace("asp", "ASP").Replace("Asp", "ASP")
                        .Replace("git", "Git")
                        .Replace("vue", "Vue")
                        .Replace("tomcat", "Tomcat")
                        .Replace("thinkPHP", "ThinkPHP")
                        .Replace("yii", "Yii")
                        .Replace("ci", "CI")
                        .Replace("JAVA开发工程师", "").Replace("PHP工程师", "").Replace("PHP开发", "").Replace("NET开发工程师", "").Replace("NET工程师", "")
                        );
                }
                // 分词
                var segs = seg.Cut(sb.ToString());

                // 测试用
                using (StreamWriter sw = new StreamWriter("D://mydict.txt"))
                {
                    foreach (string s in segs)
                    {
                        sw.WriteLine(s);
                    }
                }

                // 词频统计
                var freqs = new Counter<string>(segs);
                // 根据已有的分词，取前30个出来
                TfidfExtractor extractor = new TfidfExtractor();
                var wordWeight = extractor.ExtractTagsWithWeight(string.Join("/", segs), 30);//权重
                StringBuilder sbr = new StringBuilder();
                using(StreamWriter sw = new StreamWriter("D://" + arr[i] + "权重.txt"))
                {
                    foreach (var item in wordWeight)
                    {
                        sw.WriteLine(item.Word+ ","+ item.Weight+ ",");
                    }
                }
                
                var t = extractor.ExtractTags(string.Join("/", segs).Trim(), 30, null);//频率
                using (StreamWriter sw = new StreamWriter("D://" + arr[i] + ".txt"))
                {
                    sw.WriteLine("[");
                    foreach (string s in t)
                    {
                        sw.WriteLine("{\"name\":\""+ s + "\",\"value\":" + freqs[s] + "},");
                    }
                    sw.WriteLine("]");
                }
                i++;
            }


            //ViewBag.endTxt = string.Join("\t",t);
        }
    }
}